# coding:utf-8
# @Time    : 2021/12/8 16:56
# @Author  : Martin
from page_object.detail_page import DetailPage
from page_object.base.page_items import PageItems


class QueryPage(PageItems):
    _base_url = "/nspas-web-in/queryDetail.do"

    def get_act_code(self, business_code):
        """
        根据公文号查询活动，获取活动编码
        :param business_code:
        :return:
        """
        self.input('name', 'activityQueryRequest.businessCode', business_code)
        self.click('xpath', '//input[@value="查询"]')
        self.wait(1)
        act_code = self._get_act_code()
        num = 0
        while not act_code:
            num += 1
            self.logger.info('未查询到活动编码，等待5秒，继续查询')
            self.wait(5)
            self.click('xpath', '//input[@value="查询"]')
            act_code = self._get_act_code()
            if act_code:
                self.logger.info(f'查询到活动编码<{act_code}>')
                return act_code
            elif num > 6:
                self.logger.warning(f'等待查询到活动编码超过次数，结束查询，返回活动编码为空')
                return ''
        return act_code

    def _get_act_code(self):
        return self.get_element_attr('xpath', '//table[@class="detailContent"]//input[contains(@id,"activityCode")]','value')

    def assert_act_code(self, act_code):
        assert self.assert_text_by_re(act_code, '[0-9]{18}'), f'断言失败，预期结果是18位编码，实际结果为：{act_code}'

    def query_act(self, business_code):
        self.input('name', 'activityQueryRequest.businessCode', business_code)
        self.click('xpath', '//input[@value="查询"]')

    def go_to_detail(self, business_code):
        self.click('link text', business_code)
        self.switch_current_handle()
        return DetailPage(self.driver, self.logger)
    # def click_submit(self,node):
    #     self.click('link text', '提交')
    #     self.switch_current_handle()

